<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
 * @method static create(array $validated)
 * @method static exists($id)
 * @method static where(string $string, $id)
 * @method static find($id)
 */
class Post extends Model
{
    use HasFactory;

    protected $table = "tb_posts";
    protected $primaryKey = "post_id";
    public const CREATED_AT = "post_create_time";
    public const UPDATED_AT = "post_update_time";
    protected $fillable = [
        "post_name", "post_describe", "post_cover", "cate_id",
        "post_is_top", "post_is_encrypted", "post_status"
    ];

    public function detail(): HasOne
    {
        return $this->hasOne("App\Models\PostDetail", "post_id");
    }

    public function category(): BelongsTo
    {
        return $this->belongsTo("App\Models\Category", "cate_id", "cate_id");
    }

    public function tags(): BelongsToMany
    {
        return $this->belongsToMany("App\Models\Tag", "tb_tag_post", "post_id", "tag_id");
    }

    public function comments(): BelongsTo
    {
        return $this->belongsTo("App\Models\Comment", "post_id", "post_id");
    }
}
